package pe.edu.upc.e41a.javasoft.jsf;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.validator.LengthValidator;
import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.message.Message;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.edu.upc.e41a.javasoft.domain.TipoDocumento;
import pe.edu.upc.e41a.javasoft.domain.Usuario;
import pe.edu.upc.e41a.javasoft.service.TipoDocumentoService;

@Configurable
@ManagedBean(name = "tipoDocumentoBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = TipoDocumento.class, beanName = "tipoDocumentoBean")
public class TipoDocumentoBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
    TipoDocumentoService tipoDocumentoService;

	private String name = "TipoDocumentoes";

	private TipoDocumento tipoDocumento;

	private List<TipoDocumento> allTipoDocumentoes;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<Usuario> selectedUsuarios;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("descripcion");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<TipoDocumento> getAllTipoDocumentoes() {
        return allTipoDocumentoes;
    }

	public void setAllTipoDocumentoes(List<TipoDocumento> allTipoDocumentoes) {
        this.allTipoDocumentoes = allTipoDocumentoes;
    }

	public String findAllTipoDocumentoes() {
        allTipoDocumentoes = tipoDocumentoService.findAllTipoDocumentoes();
        dataVisible = !allTipoDocumentoes.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText usuariosCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuariosCreateOutput.setId("usuariosCreateOutput");
        usuariosCreateOutput.setValue("Usuarios:   ");
        htmlPanelGrid.getChildren().add(usuariosCreateOutput);
        
        HtmlOutputText usuariosCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuariosCreateInput.setId("usuariosCreateInput");
        usuariosCreateInput.setValue("This relationship is managed from the Usuario side");
        htmlPanelGrid.getChildren().add(usuariosCreateInput);
        
        Message usuariosCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        usuariosCreateInputMessage.setId("usuariosCreateInputMessage");
        usuariosCreateInputMessage.setFor("usuariosCreateInput");
        usuariosCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(usuariosCreateInputMessage);
        
        HtmlOutputText descripcionCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionCreateOutput.setId("descripcionCreateOutput");
        descripcionCreateOutput.setValue("Descripcion: * ");
        htmlPanelGrid.getChildren().add(descripcionCreateOutput);
        
        InputTextarea descripcionCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        descripcionCreateInput.setId("descripcionCreateInput");
        descripcionCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{tipoDocumentoBean.tipoDocumento.descripcion}", String.class));
        LengthValidator descripcionCreateInputValidator = new LengthValidator();
        descripcionCreateInputValidator.setMaximum(50);
        descripcionCreateInput.addValidator(descripcionCreateInputValidator);
        descripcionCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(descripcionCreateInput);
        
        Message descripcionCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        descripcionCreateInputMessage.setId("descripcionCreateInputMessage");
        descripcionCreateInputMessage.setFor("descripcionCreateInput");
        descripcionCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(descripcionCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText usuariosEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuariosEditOutput.setId("usuariosEditOutput");
        usuariosEditOutput.setValue("Usuarios:   ");
        htmlPanelGrid.getChildren().add(usuariosEditOutput);
        
        HtmlOutputText usuariosEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuariosEditInput.setId("usuariosEditInput");
        usuariosEditInput.setValue("This relationship is managed from the Usuario side");
        htmlPanelGrid.getChildren().add(usuariosEditInput);
        
        Message usuariosEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        usuariosEditInputMessage.setId("usuariosEditInputMessage");
        usuariosEditInputMessage.setFor("usuariosEditInput");
        usuariosEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(usuariosEditInputMessage);
        
        HtmlOutputText descripcionEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionEditOutput.setId("descripcionEditOutput");
        descripcionEditOutput.setValue("Descripcion: * ");
        htmlPanelGrid.getChildren().add(descripcionEditOutput);
        
        InputTextarea descripcionEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        descripcionEditInput.setId("descripcionEditInput");
        descripcionEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{tipoDocumentoBean.tipoDocumento.descripcion}", String.class));
        LengthValidator descripcionEditInputValidator = new LengthValidator();
        descripcionEditInputValidator.setMaximum(50);
        descripcionEditInput.addValidator(descripcionEditInputValidator);
        descripcionEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(descripcionEditInput);
        
        Message descripcionEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        descripcionEditInputMessage.setId("descripcionEditInputMessage");
        descripcionEditInputMessage.setFor("descripcionEditInput");
        descripcionEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(descripcionEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText usuariosLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuariosLabel.setId("usuariosLabel");
        usuariosLabel.setValue("Usuarios:   ");
        htmlPanelGrid.getChildren().add(usuariosLabel);
        
        HtmlOutputText usuariosValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuariosValue.setId("usuariosValue");
        usuariosValue.setValue("This relationship is managed from the Usuario side");
        htmlPanelGrid.getChildren().add(usuariosValue);
        
        HtmlOutputText descripcionLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        descripcionLabel.setId("descripcionLabel");
        descripcionLabel.setValue("Descripcion:   ");
        htmlPanelGrid.getChildren().add(descripcionLabel);
        
        InputTextarea descripcionValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        descripcionValue.setId("descripcionValue");
        descripcionValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{tipoDocumentoBean.tipoDocumento.descripcion}", String.class));
        descripcionValue.setReadonly(true);
        descripcionValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(descripcionValue);
        
        return htmlPanelGrid;
    }

	public TipoDocumento getTipoDocumento() {
        if (tipoDocumento == null) {
            tipoDocumento = new TipoDocumento();
        }
        return tipoDocumento;
    }

	public void setTipoDocumento(TipoDocumento tipoDocumento) {
        this.tipoDocumento = tipoDocumento;
    }

	public List<Usuario> getSelectedUsuarios() {
        return selectedUsuarios;
    }

	public void setSelectedUsuarios(List<Usuario> selectedUsuarios) {
        if (selectedUsuarios != null) {
            tipoDocumento.setUsuarios(new HashSet<Usuario>(selectedUsuarios));
        }
        this.selectedUsuarios = selectedUsuarios;
    }

	public String onEdit() {
        if (tipoDocumento != null && tipoDocumento.getUsuarios() != null) {
            selectedUsuarios = new ArrayList<Usuario>(tipoDocumento.getUsuarios());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllTipoDocumentoes();
        return "tipoDocumento";
    }

	public String displayCreateDialog() {
        tipoDocumento = new TipoDocumento();
        createDialogVisible = true;
        return "tipoDocumento";
    }

	public String persist() {
        String message = "";
        if (tipoDocumento.getCodigo() != null) {
            tipoDocumentoService.updateTipoDocumento(tipoDocumento);
            message = "Successfully updated";
        } else {
            tipoDocumentoService.saveTipoDocumento(tipoDocumento);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllTipoDocumentoes();
    }

	public String delete() {
        tipoDocumentoService.deleteTipoDocumento(tipoDocumento);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllTipoDocumentoes();
    }

	public void reset() {
        tipoDocumento = null;
        selectedUsuarios = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }
}
